home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / gold / bubMethods.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.9 KB  |  246 lines

  1. /*
  2.  * The original copyright owners of the accompanying source code files have
  3.  * agreed to place such code into the public domain.  Accordingly, anyone
  4.  * who receives or obtains a copy of such source code is freely entitled to
  5.  * reproduce, use and otherwise exploit such code (including the right to
  6.  * make derivative works), at his/her own risk and expense, without any
  7.  * obligation or liability to the original copyright owners.
  8.  *
  9.  * We would appreciate (but do not require) that the following message be
  10.  * included in any derivative works:
  11.  *
  12.  * "Portions of this program were developed by Peter Broadwell, Rob Myers
  13.  * and Robin Schaufler while working in Silicon Valley."
  14.  *
  15.  * The accompanying source code files and related documentation materials
  16.  * are distributed on an "AS IS" basis, without any warranties or
  17.  * guarantees of any kind.  All implied warranties, including the implied
  18.  * warranties of merchantability and of fitness for any particular purpose,
  19.  * are expressly disclaimed.
  20.  */
  21. #include <stdio.h>
  22. #include <math.h>
  23. #include "gl.h"
  24. #include "device.h"
  25. #include "geom.h"
  26. #include "class.h"
  27. #include "selectors.h"
  28. #include "classIds.h"
  29. #include "mbox.h"
  30. #include "individual.h"
  31. #include "behavior.h"
  32. #include "doers.h"
  33. #include "bub.h"
  34. #include "sea.h"
  35. #include "pick.h"
  36. #include "colors.h"
  37.  
  38. extern individual *us;
  39. extern individual bubbleTemplate;
  40. extern behavior advancerTemplate;
  41. extern behavior expanderTemplate;
  42. extern behavior surfDieTemplate;
  43. extern individual *curIndiv;
  44.  
  45. #define MAXBUBBLES 125
  46.  
  47. individual * bubStack[MAXBUBBLES];
  48. static long bubcount;
  49. static long newbub;
  50. extern int inNothing;
  51. extern int controls;
  52.  
  53. behavior *curBehave = NULL;
  54.  
  55.     /*
  56.      * cough up a new bubble (or recycle an old dead one)
  57.      */
  58.  
  59.     individual *
  60. makeBub()
  61. {
  62.     individual *bub;
  63.  
  64.     if(bubcount == 0) {
  65.     newbub = TRUE;
  66.     return (individual *)clone(&bubbleTemplate);
  67.     }
  68.     else {
  69.     newbub = FALSE;
  70.     bub = bubStack[bubcount--];
  71.     bub->velocity.z = (&bubbleTemplate)->velocity.z;
  72.     return bub;
  73.     }
  74. }
  75.  
  76. eatBub(bub)
  77.     individual * bub;
  78. {
  79.     if(++bubcount >= MAXBUBBLES) {
  80.     fprintf(stderr,"trying to eat to much\n");
  81.     return;
  82.     }
  83.     bubStack[bubcount] = bub;
  84. }
  85.  
  86.     /*
  87.      * pop this bubble, store for reuse
  88.      */
  89. bubblefree(self)
  90.     register individual *self;
  91. {
  92.     register subscr *s;
  93.  
  94.     dumpSubscribers("bubble's subscribers", self->them.subscribers, 0);
  95.     while (s = self->them.subscribers)
  96.     unsubscribe(self, s->member);
  97.  
  98.     dumpSubscribers("bubble's subscribedTos", self->them.subscribedTo, 0);
  99.     while (s = self->them.subscribedTo)
  100.     unsubscribe(s->member, self);
  101.  
  102.     if (self == curIndiv)
  103.     curIndiv = NULL;
  104.  
  105. /**********
  106.     freeMsegs(self->descr);
  107. ***********/
  108.     eatBub(self);
  109. }
  110.  
  111.     /*
  112.      *  init the bubble machine
  113.      */
  114. /* ARGSUSED */
  115.     bubbleMachine *
  116. blowInit(s)
  117.     register bubbleMachine *s;
  118. {
  119.     /*********
  120.     behavior **pb;
  121.  
  122.     for (pb = s->doers; pb && *pb; pb++) {
  123.     if ((*pb)->them.me.myClass)
  124.         printf("bub templ %d\n", (*pb)->them.me.myClass->classId);
  125.     else
  126.         printf("bub templ has null myClass ptr\n");
  127.     }
  128.     **********/
  129. }
  130.  
  131.     /*
  132.      *  blow a new bubble
  133.      */
  134.     bubbleMachine *
  135. blowBubble(s)
  136.     register bubbleMachine *s;
  137. {
  138.     register bubble *bub;
  139.     expanderVars *vars;
  140.     bubble *templ;
  141.     behavior *doer;
  142.  
  143.     if (s->bubbleDispatchIncr && s->bubbleClock % s->bubbleDispatchIncr == 0) {
  144.     bub = (bubble *)makeBub();
  145.     subscribe(us, bub);
  146.     doer = s->doers[s->bubbleDoer];
  147.     templ = (bubble *)doer->bubtemplate;
  148.     if (templ->doer != doer) {
  149.         printf("blow: doer = %x,%d, templ->doer = %x,%d\n",
  150.               doer,
  151.               doer->them.me.myClass->classId,
  152.               templ->doer,
  153.               ((behavior *)templ->doer)->them.me.myClass->classId);
  154.     }
  155.     *bub->me.descr = *templ->me.descr;
  156.     bub->doer = templ->doer;
  157.  
  158.     subscribe(&advancerTemplate, bub);
  159.     subscribe(&expanderTemplate, bub);
  160.     subscribe(&surfDieTemplate, bub);
  161.     Msg(bub, INIT, NOARG, NULL);
  162.  
  163.     if (vars = (expanderVars *)findvars(bub, EXPANDER)) {
  164.         /* bubbles will expand to different sizes */
  165.         vars->scaleMax = abs((float)sin(s->bubbleClock/3.5) * 40.0) +
  166.                  10.0;
  167.  
  168.         /* some bubbles go faster */
  169.         if (s->bubbleSpeedupIncr &&
  170.         s->bubbleClock % s->bubbleSpeedupIncr == 0)
  171.             bub->me.velocity.z += vars->scaleMax * 1.3;
  172.     }
  173.  
  174.     bub->me.position.x = s->me.position.x;
  175.     bub->me.position.y = s->me.position.y;
  176.     bub->me.position.z = s->me.position.z;
  177.  
  178.     bub->me.lastPosition.x = bub->me.position.x - bub->me.velocity.x;
  179.     bub->me.lastPosition.y = bub->me.position.y - bub->me.velocity.y;
  180.     bub->me.lastPosition.z = bub->me.position.z - bub->me.velocity.z;
  181.     /* printf("blowBubble:    new bubble 0x%x\n", bub); /* */
  182.  
  183.     if (s->doers[++s->bubbleDoer] == NULL)
  184.         s->bubbleDoer = 0;
  185.     }
  186.     s->bubbleClock++;
  187.     return s;
  188. }
  189.  
  190.     /*
  191.      *  initialize a bubble's drawing
  192.      */
  193. bubbleinit(s)
  194.     register individual *s;
  195. {
  196.     register model *myModel;
  197.  
  198.     /* first, instantiate and initialize the model */
  199.     if(newbub) replicateModel(s);
  200.     myModel = s->descr;    /* use newly instantiated model */
  201.  
  202.     makeobj((Object)myModel);
  203.     pushLongName(s);
  204.     backface(FALSE);
  205.     fishWritemask(FOREGROUND);
  206.     fishColor(myModel->color);
  207.     scale(-1.0, 1.0, -1.0);
  208. #define WIMP
  209. #ifdef WIMP
  210.     rotate(900,'z');
  211.     rotate(900,'y');
  212. #endif /* WIMP */
  213.     circi(10,0,12);
  214.     circfi(7,4,2);
  215.     backface(TRUE);
  216.     fishWritemask(NOGROUND);
  217.     rectfi(-4,14,24,-14);
  218.     popLongName();
  219.     closeobj();
  220.     myModel->compiled = TRUE;
  221.     myModel->component = (Object)myModel;
  222. }
  223.  
  224. /* ARGSUSED */
  225. selectBub(self, argtype, hit)
  226.     register bubble *self;
  227.     long argtype;
  228.     hitstruct *hit;
  229. {
  230.     inNothing = FALSE;
  231.  
  232.     if (self->doer != curBehave) {
  233.     }
  234.     curBehave = self->doer;
  235.     if (! curBehave) {
  236.     fprintf(stderr,"Got a bub with NULL doer\n");
  237.     return;
  238.     }
  239.     /* printf("Got a bub for %d\n", self->doer->them.me.myClass->classId); /* */
  240.     if (curIndiv)
  241.     Msg(curIndiv, POSTBEHAVE, sizeof(behavior), curBehave);
  242.     else        /* send notice to us on behalf of no current indiv */
  243.     Msg(us, POSTBEHAVE, sizeof(behavior), curBehave);
  244.     controls = TRUE;
  245. }
  246.